from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import Qt, QString

#
# this is one of those little time-stamped buttons
# that represent events
#
# no reason to have it in its own class, except
# that it uses QPainterPath and I want to test it in isolation
#

class MsgCell_Time(QtGui.QWidget):
        def __init__(self, *args, **kw):
		QtGui.QWidget.__init__(self, *args, **kw)
		self.set_time(740)
		self.toggle_state = False

	def paintEvent(self, e):

		pen = QtGui.QPen( Qt.black, 1, 
			Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin
		);

		font = QtGui.QFont("Times New Roman", 18);
		baseline = QtCore.QPointF(14, 21);

		pp = QtGui.QPainterPath()
		pp.addText(baseline, font, "{0:02d}:{1:02d}".format(
			self.hh, self.mm
		));

		qp = QtGui.QPainter (self);
		col = None
		if self.toggle_state:
			col = QtGui.QColor(Qt.yellow)
		else:
			col = QtGui.QColor(Qt.transparent)
		#print "painting: ", col
		qp.setBrush(col);
		qp.setPen(pen);
		qp.setRenderHint(QtGui.QPainter.Antialiasing, True)
		qp.setRenderHint(QtGui.QPainter.TextAntialiasing, True)
		qp.setRenderHint(
			QtGui.QPainter.HighQualityAntialiasing, True
		)
		qp.drawPath(pp);

	def set_time(self, hhmm):
		self.mm = hhmm % 100
		self.hh = (hhmm - self.mm) / 100

	def set_toggle_state(self, bval):
		self.toggle_state = bval
		self.repaint()

	def toggle(self):
		self.toggle_state = not self.toggle_state
		self.repaint()

class MsgCell(QtGui.QWidget):
        def __init__(self, *args, **kw):
		QtGui.QWidget.__init__(self, *args, **kw)

		bg = QtGui.QLabel(self)
		bg.setGeometry(0, 0, 90, 25)
		path = "resources/msg_main_time.png"
		bg.setPixmap(QtGui.QPixmap(path))

		self.time = MsgCell_Time(self)
		self.ev = None

	def mousePressEvent(self, e):
		self.time.toggle()
		self.clicked.emit(self, self.time.toggle_state)

	def set_time(self, hhmm):
		self.time.set_time(hhmm)

	def set_toggle_state(self, bval):
		self.time.set_toggle_state(bval)

	def toggle(self):
		self.time.toggle()

	clicked = QtCore.pyqtSignal(object, bool)



if __name__ == '__main__':
	import sys
	app = QtGui.QApplication(sys.argv)

#
#	use the wallpaper as the top level widget
#
	top = QtGui.QLabel()
	top.setGeometry(0, 0, 160, 100)
	path = "resources/wood.jpg"
	top.setPixmap(QtGui.QPixmap(path))
	

	cell = MsgCell(top)
	cell.move(20,20)
	cell.set_time(120)

	cell = MsgCell(top)
	cell.move(20,50)
	cell.set_time(2310)

	top.show()
	sys.exit(app.exec_())
